package com.rlj.messagequeue.mqserver.core;

import java.io.Serializable;
import java.util.UUID;

/**
 * Created with IntelliJ IDEA.
 * Description: 消息
 * User: DELL
 * Date: 2024-02-19
 * Time: 23:47
 */
public class Message implements Serializable {

    private static final Long serialVersionUID = 1L;

    // 基本属性
    private BasicProperties basicProperties = new BasicProperties();
    // 数据（使用byte数组可以支持字符和二进制数据）
    private byte[] body;

    // 辅助属性
    // 偏移量，标记消息在文件中的位置，[offsetBeg,offsetEnd)
    // 序列化的时候不需要序列化偏移量
    private transient long offsetBeg = 0;     // 消息数据的开头距离文件开头的位置偏移(字节)
    private transient long offsetEnd = 0;     // 消息数据的结尾距离文件开头的位置偏移(字节)
    // 消息是否有效（针对文件中的消息进行逻辑删除）
    private byte isValid = 0x1;

    // 创建 Message 对象的工厂方法
    public static Message createMessageWithId(String routingKey, BasicProperties basicProperties, byte[] body) {
        Message message = new Message();
        if (basicProperties != null) {
            message.setBasicProperties(basicProperties);
        }
        message.srtMessageId("M-" + UUID.randomUUID());
        message.srtRoutingKey(routingKey);
        message.body = body;

        return message;
    }

    public String getMessageId() {
        return basicProperties.getMessageId();
    }

    public void srtMessageId(String messageId) {
        basicProperties.setMessageId(messageId);
    }

    public String getRoutingKey() {
        return basicProperties.getRoutingKey();
    }

    public void srtRoutingKey(String routingKey) {
        basicProperties.setRoutingKey(routingKey);
    }

    public int getDeliverMode() {
        return basicProperties.getDeliverMode();
    }

    public void setDeliverMode(int deliverMode) {
        basicProperties.setDeliverMode(deliverMode);
    }

    public BasicProperties getBasicProperties() {
        return basicProperties;
    }

    public void setBasicProperties(BasicProperties basicProperties) {
        this.basicProperties = basicProperties;
    }

    public byte[] getBody() {
        return body;
    }

    public void setBody(byte[] body) {
        this.body = body;
    }

    public long getOffsetBeg() {
        return offsetBeg;
    }

    public void setOffsetBeg(long offsetBeg) {
        this.offsetBeg = offsetBeg;
    }

    public long getOffsetEnd() {
        return offsetEnd;
    }

    public void setOffsetEnd(long offsetEnd) {
        this.offsetEnd = offsetEnd;
    }

    public byte getIsValid() {
        return isValid;
    }

    public void setIsValid(byte isValid) {
        this.isValid = isValid;
    }
}
